﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Core.Graphics;

namespace WorldEditor.Tiles
{
    public partial class TilePallets : UserControl
    {
        public event EventHandler SelectedTileChanged;
        public Pallet CurrentPallet { get; protected set; }

        public TilePallets()
        {
            InitializeComponent();

            UpdateTileSetList();
        }

        private void cbTiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool flag = false;
            TileSet ts = (TileSet)cbTiles.SelectedItem;

            foreach (Control c in tlpDrawing.Controls)
            {
                if (c is Pallet)
                {
                    Pallet pl = (Pallet)c;
                    if (pl.TileSet == ts)
                    {
                        pl.Visible = true;
                        flag = true;
                        CurrentPallet = pl;
                    }
                    else
                    {
                        pl.Visible = false;
                    }
                }
            }

            if (!flag)
            {
                LoadTileSetBitmaps(ts);
            }

            //set pallet layout display
            SetPalletDisplayStyle();
        }

        protected void LoadTileSetBitmaps(TileSet ts)
        {
            Pallet pallet1 = new Pallet(ts);

            pallet1.Location = new System.Drawing.Point(7, 30);
            pallet1.Dock = DockStyle.Fill;
            //pallet1.Size = new System.Drawing.Size(157, 419);

            pallet1.TileSelected += new EventHandler(PalletWindow_SelectedTileChanged);

            tlpDrawing.Controls.Add(pallet1, 0, 1);

            CurrentPallet = pallet1;
        }

        protected void PalletWindow_SelectedTileChanged(object sender, EventArgs e)
        {
            TileSetTile pt = ((PalletTile)sender).GetUnderlyingTile;

            if (SelectedTileChanged.Method != null)
                SelectedTileChanged(pt, EventArgs.Empty);
        }

        public void UpdateTileSetList()
        {
            cbTiles.Items.Clear();

            foreach (KeyValuePair<int, TileSet> ts in TileSetCollection.GlobalTerrainTileSets)
            {
                cbTiles.Items.Add(ts.Value);
            }
        }

        private void pbRefresh_Click(object sender, EventArgs e)
        {
            UpdateTileSetList();
        }

        private void cbProperLayout_CheckedChanged(object sender, EventArgs e)
        {
            //on layout checked changed.
            SetPalletDisplayStyle();
        }

        private void SetPalletDisplayStyle()
        {
            //if we want proper layout, then set the currently selected pallet width
            if (cbProperLayout.Checked)
            {
                //don't dock
                CurrentPallet.Dock = DockStyle.None;

                CurrentPallet.Width = CurrentPallet.TileSet.TilesPerRow * 37;
                CurrentPallet.Height = CurrentPallet.TileSet.TilesPerColumn * 37;
            }
            else
            {
                //just dock it
                CurrentPallet.Dock = DockStyle.Fill;
            }
        }
    }
}
